home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Scan / 32-bit_x86 / Francais / cpsimage.cab / data / xipProcs / hlExtract.proc < prev    next >
Text File  |  2009-03-16  |  7KB  |  208 lines

  1. // Load scripted support procedures
  2. #load "xipProcs/morphology.proc";
  3.  
  4. /* @XIPHighlightExtract
  5.   // DESCRIPTION
  6.   This procedure extracts highlighted text regions from a color image. It can
  7.   extract text regions for multiple highlighter colors, and return each text
  8.   extraction as a separate image or merge the extractions into a single image.
  9.   The resulting image(s) will be returned in the ELFLIST object "output".
  10.   
  11.   // ARGUMENTS
  12.   XIPIMAGE input      Image to extract highlighted text regions from
  13.   
  14.   INTEGER  merge      This specifies the output format:
  15.                         0 to keep each text extraction as a separate image
  16.   
  17.                         1 to combine each text extraction image using the AND
  18.                           operator into a single text extraction image
  19.   
  20.   // RETURNS
  21.   ELFLIST output      This is a list of XIPIMAGE objects representing the
  22.                       extracted highlighted text images. The info member of
  23.                       each XIPIMAGE is a string indicating the highlighter
  24.                       color(s) that covered the text.
  25. */
  26. PROCEDURE XIPHighlightExtract (XIPIMAGE input, INTEGER merge)
  27.   RETURNS (ELFLIST output)
  28. {
  29.   XIPIMAGE   imgBase, imgText, imgHL, imgHLText;
  30.   STRING     colorname, str, tmp;
  31.   STRING     se5x5, se7x1;
  32.   INTEGER    i, skipOne, nOutputs;
  33.   INTEGER    minlevel, maxlevel;
  34.   INTEGER    x, y, w, h;
  35.   ELFLIST    win;
  36.   DOUBLE     avg;
  37.  
  38.   // Import IPCore definitions
  39.   #import "ipcore2xip.ucm";
  40.  
  41.   // Create structuring element strings
  42.   se5x5 = XIPMakeSolidStrel (width: 5, height: 5).toString ();
  43.   se7x1 = XIPMakeSolidStrel (width: 7, height: 1).toString ();
  44.  
  45.   // Find highlighter color regions
  46.   str = input.hlcinfo (
  47.           ).toString (
  48.           ).match (str: "color:"
  49.           );
  50.  
  51.   // Process highlighter color regions
  52.   skipOne = 0;
  53.   for (tmp=str.getToken (); tmp; tmp=str.getToken ())
  54.     if (tmp == "color:") {
  55.       if (!skipOne) {
  56.         str.getLine ();
  57.         skipOne++;
  58.       } else {
  59.         // Parse highlighter color name
  60.         colorname = str.getToken ();
  61.  
  62.         // Parse out region window
  63.         str.getToken ();
  64.         x = str.getToken ();
  65.         y = str.getToken ();
  66.         w = str.getToken ();
  67.         h = str.getToken ();
  68.         win = (x, y, w, h);
  69.  
  70.         // Determine highlighter minimum and maximum levels
  71.         if (colorname == "orange") {
  72.           minlevel = 17;
  73.           maxlevel = 17;
  74.         } else if (colorname == "yellow") {
  75.           minlevel = 34;
  76.           maxlevel = 51;
  77.         } else if (colorname == "spring") {
  78.           minlevel  = 68;
  79.           maxlevel  = 68;
  80.         } else if (colorname == "green") {
  81.           minlevel = 85;
  82.           maxlevel = 85;
  83.         } else if (colorname == "teal") {
  84.           minlevel = 102;
  85.           maxlevel = 102;
  86.         } else if (colorname == "cyan") {
  87.           minlevel = 119;
  88.           maxlevel = 136;
  89.         } else if (colorname == "azure") {
  90.           minlevel = 153;
  91.           maxlevel = 153;
  92.         } else if (colorname == "blue") {
  93.           minlevel = 170;
  94.           maxlevel = 170;
  95.         } else if (colorname == "violet") {
  96.           minlevel = 187;
  97.           maxlevel = 187;
  98.         } else if (colorname == "magenta") {
  99.           minlevel  = 204;
  100.           maxlevel  = 238;
  101.         } else if (colorname == "pink") {
  102.           minlevel  = 204;
  103.           maxlevel  = 238;
  104.         } else if (colorname == "red") {
  105.           minlevel = 255;
  106.           maxlevel = 255;
  107.         }
  108.  
  109.         imgBase = input.crop (window: win
  110.                     ).interleave (scanline: 1
  111.                     ).cspace (outspace: "hsv", precise: 1
  112.                     );
  113.  
  114.         // Threshold for text (text image)
  115.         imgText = imgBase.levels (levels: (12, 4, 4)
  116.                     ).median (filter: "2 2 1 1 1 1"
  117.                     ).range (lower: (0, 0, 0), upper: (255, 255, 85),
  118.                              pixel: 1, incolor: (0, 0, 0), outcolor: (0, 0, 255)
  119.                     ).channel (parts: "3"
  120.                     ).convert (to: (1)
  121.                     ).dilate_ipc (
  122.                     ).erode_ipc (
  123.                     );
  124.  
  125.         // Threshold for highlighter color (highlighter image)
  126.         imgHL = imgBase.range (lower: (0, 32, 64), upper: (255, 255, 255),
  127.                                pixel: 1, ingray: 1
  128.                   ).levels (levels: (12, 4, 4)
  129.                   ).median (filter: "2 2 1 1 1 1"
  130.                   ).range (lower: (minlevel, 85, 85),
  131.                            upper: (maxlevel, 255, 255)
  132.                   ).channel (parts: "1"
  133.                   ).convert (to: (1)
  134.                   ).subsample (reduce: 4
  135.                   ).convert (to: (1)
  136.                   ).invert (
  137.                   ).dilate_ipc (sestring: se5x5
  138.                   ).erode_ipc (sestring: se5x5
  139.                   ).erode_ipc (
  140.                   ).dilate_ipc (
  141.                   ).scale (awindow: (win[2], win[3])
  142.                   ).convert (to: (1)
  143.                   );
  144.  
  145.         // Add text image to mask channel of highlighter image
  146.         imgHL = imgHL.addLayer (image: imgText, lindex: 0,
  147.                                 ltype: XIP_ColorMask);
  148.  
  149.         // Seed fill to get the text covered by highlighter
  150.         imgHLText = imgHL.seedfill (iterations: 3);
  151.  
  152.         // Continue if percent average is 100% (no highlighted text in region)
  153.         avg = getPercentAvg (inImg: imgHLText.convert (to: (8)));
  154.         if (avg == 1.00)
  155.           continue;
  156.  
  157.         // Label extraction image
  158.         imgHLText.info = "Highlighter: " + colorname;
  159.  
  160.         // Add extraction image to output list
  161.         output.insert (obj: imgHLText);
  162.       }
  163.     }
  164.  
  165.   // Merge extraction images if merge option is specified
  166.   nOutputs = output.length ();
  167.   if (merge && (nOutputs > 1)) {
  168.     XIPIMAGE img, next;
  169.     STRING   label;
  170.     img = output.extract (entry: 0);
  171.  
  172.     for (i=1; i<nOutputs; i++) {
  173.       next = output.extract (entry: 0);
  174.       img = img.math (operator: "and", input: next);
  175.  
  176.       label = next.info;
  177.       label.getToken ();
  178.       img.info = img.info + ", " + label.getToken ();
  179.     }
  180.     output.insert (obj: img);
  181.   }
  182. }
  183.  
  184.  
  185.  
  186. /* @getPercentAvg
  187.   // DESCRIPTION
  188.   Given a text extraction image, return the average percent intensity. It uses
  189.   XIPIMAGE method "avginfo" to capture image parameters of min, max, and
  190.   average
  191. */
  192. private
  193. PROCEDURE getPercentAvg (XIPIMAGE inImg)
  194.   RETURNS (DOUBLE percent)
  195. {
  196.   INTEGER  j;
  197.  
  198.   // Get average percent intensity of text extraction image
  199.   STRING info = inImg.avginfo ().toString ();
  200.  
  201.   // Jump to "Entry 0" in "info" string then jump 13 tokens to get percent
  202.   info.match (str: "Entry 0:");
  203.   for (j=0; j<13; j++)
  204.     info.getToken ();
  205.  
  206.   percent = info.getToken ();
  207. }
  208.